<?php
	header("Content-type: text/html;charset=utf8");
	require_once("../../db/dbclass.php");
	require_once("../../utils/common.php");
	$myDb=&DbController::getInstance();
	$type=$_POST['type'];
	$array=array();
	switch ($type) {
		case '1':
			$content=$_POST['content'];
			$array_content=explode(";", $content);
			foreach ($array_content as $value) {
				if($value==""){
					break;
				}
				$param="select * from yj_sensitive_words where sensitivity_value='".$value."'";
				$row = mysqli_num_rows($myDb->exec_query($param));
				if($row==0){
					$param="INSERT INTO yj_sensitive_words (sensitivity_value) VALUES('".$value."')";
					$result=$myDb->exec_query($param);
					if ($result==TRUE) {
						// $arrayName = array(getFirstCharter($value) => $value);
						$array[getFirstCharter($value)]=$value;
						continue;
					}else{
						$arr = array('result' =>"0",
							'array_content'=> $array);
						echo json_encode($arr);
						return;
					}
				}
			}
			break;
		case '2':
			$content=$_POST['content'];
			$array_content=explode(";", $content);
			foreach ($array_content as $value) {
				if($value==""){
					break;
				}
				$param="select * from yj_sensitive_words where sensitivity_value='".$value."'";
				$result=$myDb->exec_query($param);
				if($result==FALSE){
					$arr = array('result' =>"0",
							'array_content'=> $array);
					echo json_encode($arr);
					return;
				}
				$row = mysqli_num_rows($result);
				if($row==0){
					break;
				}
				else{
					$param="DELETE FROM yj_sensitive_words WHERE sensitivity_value='".$value."'";
					$result=$myDb->exec_query($param);
					if ($result==TRUE) {
						$array[getFirstCharter($value)]=$value;
						continue;
					}else{
						$arr = array('result' =>"0",
							'array_content'=> $array);
						echo json_encode($arr);
						return;
					}
				}
			}
			break;
		default:
			$param="select * from yj_sensitive_words";
			$result=$myDb->exec_query($param);
			// $row = mysqli_num_rows($myDb->exec_query($param));
			while ($row=mysqli_fetch_array($result,MYSQL_ASSOC)) {
				if(!isset($array[getFirstCharter($row['sensitivity_value'])])){
					$array[getFirstCharter($row['sensitivity_value'])]=$row['sensitivity_value'];
				}
				else{
					$array[getFirstCharter($row['sensitivity_value'])]=$array[getFirstCharter($row['sensitivity_value'])].";".$row['sensitivity_value'];
				}
			}
			break;
	}
	$arr = array('result' =>"1",
		'array_content'=> $array);
	echo json_encode($arr);
?>